/****************************** Module Header ******************************\

Copyright (c) Gary Davidson

This source is subject to the Microsoft Public License.
See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
All other rights reserved.

THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/
using System;
using System.Security;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Microsoft.EnterpriseManagement;
using Microsoft.EnterpriseManagement.Common;
using Microsoft.EnterpriseManagement.Configuration;
using System.ComponentModel;
using CodeSmith.Engine;
 

public class SCSMTemplateBase :CodeTemplate
	{

    private List<string> overrideList = new List<string>() {"LastModified", "ManagementGroupId", "Path","Id", "DisplayName", "Name" };	    

		public Regex MpFilterRegex = new Regex(@"ServiceManager|System|Microsoft");

		public Func<ManagementPack, bool> MpFilter()
		{
            //&& mp.Name == "ServiceManager.ConfigurationManagement.Configuration";
		    return
		        mp =>
		        mp != null 
				 && !string.IsNullOrEmpty(mp.DisplayName);
		    //&& !mp.Name.Contains("Datawarehouse") &&
		    //!mp.Name.Contains("Provance") &&
		    //!mp.Name.Contains("Linking") &&
		    //!mp.Name.Contains("ServiceDesigner") &&
		    //!mp.Name.Contains("Dell") &&
		    //!mp.Name.Contains("ConfigurationManagement") &&
		    //!mp.Name.Contains("Portal") &&
		    //!mp.Name.Contains("Administration") &&
		    //!mp.Name.Contains("OpsMgr") &&
		    //!mp.Name.Contains("Knowledge");
		}
		
		public Func<ManagementPack, bool> MpFilter2()
		{
			return
				mp =>
				mp != null ;
		}
		public Func<ManagementPack, bool> MpFilter1()
		{
			return mp => MpFilterRegex.IsMatch(mp.Name);
		}

		public string ManagementPackName(string name)
		{
			return name.Replace(".", "_");
		}

		public class IndexedList<T> : Dictionary<T, int>
		{

			public void Add(T entry)
			{
				Add(entry, 0);
			}

			public void Increment(T entry)
			{
				this[entry] +=1;    
			}

			public bool Contains(T entry)
			{
				return ContainsKey(entry);
			}

		}

		
		public List<ManagementPackRelationship> GetAllRelationships(ManagementPackClass mpc)
		{
			var allRelationships = new List<ManagementPackRelationship>();
			allRelationships.AddRange(mpc.GetRelationshipsWhereSource());

			var bts = mpc.GetBaseTypes();
			foreach (var managementPackType in bts.Cast<ManagementPackClass>())
			{
				allRelationships.AddRange(managementPackType.GetRelationshipsWhereSource());
			}
			// we do not get relationships from extension classes  - but we can! see below
			return allRelationships;
		}

		public IndexedList<string> propIndex = new IndexedList<string>();

		public EnterpriseManagementGroup GetManagementGroup(string serverName ,string userName, string password, string domain)
		{
			if(string.IsNullOrEmpty(userName))
				return new EnterpriseManagementGroup(serverName);      
			
			if(string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password)  || string.IsNullOrEmpty(password) )
			  throw new ArgumentNullException();
			
			var secureString = new SecureString();
			foreach (char c in password)
				secureString.AppendChar(c);

			secureString.MakeReadOnly();
			var settings = new EnterpriseManagementConnectionSettings(serverName)
							   {
								   UserName = userName,
								   Domain = domain,
								   Password = secureString
							   };

			return new EnterpriseManagementGroup(settings);
		}

        public bool CommonPropertyOverride(string propName)
        {
           return  overrideList.Contains(propName) ;
        }

		public void Tab()
		{
			//this.Write("\t");
		}

		public void NewLine()
		{
			//this.Write(Environment.NewLine);
		}

		public string FormatValidName(string propertyName)
		{

			return Regex.Replace(propertyName, @"[\W]", "");

		}

		public string Alias(Type type)
		{
			Dictionary<Type, string> Aliases = new Dictionary<Type, string>()
{
	{ typeof(byte), "byte" },
	 { typeof(bool), "bool?" },
	{ typeof(Guid), "Guid" }, // should never be null otherwise Guid?
	{ typeof(string), "string" },
	{ typeof(sbyte), "sbyte" },
	{ typeof(short), "short" },
	{ typeof(ushort), "ushort" },
	{ typeof(int), "int" },
	{ typeof(uint), "uint" },
	{ typeof(long), "long" },
	{ typeof(ulong), "ulong" },
	{ typeof(float), "float" },
	{ typeof(double), "double" },
	{ typeof(decimal), "decimal" },
	{ typeof(DateTime), "DateTime?" },
	{ typeof(object), "object" }
};

			if (Aliases.ContainsKey(type))
				return Aliases[type];

			if (type.Name == "System.Enum" || type.Name == "Enum")
				return "ManagementPackEnumeration";

			return type.Name;

		}




	}